package com.denlaku.longan.controller.impl;

import com.denlaku.longan.controller.CustomApiDataController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.service.CustomApiDataService;
import com.denlaku.longan.util.Lists;
import com.denlaku.longan.vo.CustomApi;
import com.denlaku.longan.vo.CustomApiData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author tianx
 */
@RestController
public class CustomApiDataControllerImpl implements CustomApiDataController {
    @Autowired
    private CustomApiDataService customApiDataService;

    @Override
    public Return<List<Map<String, Object>>> fetch(@RequestBody CustomApi customApi) {
        CustomApiData apiData = customApiDataService.fetch(customApi);
        return Resp.success(apiData.getRows());
    }

    @Override
    public Return<Map<Long, List<Map<String, Object>>>> fetches(@RequestBody List<CustomApi> customApis) {
        List<CustomApiData> list = customApiDataService.fetches(customApis);
        Map<Long, List<Map<String, Object>>> result = Lists.toMap(list, CustomApiData::getId, CustomApiData::getRows);
        return Resp.success(result);
    }

}
